package com.zcy.gift.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.mail.internet.MimeMessage;

/**
 * <p>
 *
 * </p>
 *
 * @author lucker
 * @date 2024/1/2 9:09
 */
@Component
public class MailUtil {
    @Autowired
    private JavaMailSender javaMailSender;

    @Value("${spring.mail.username}")
    private String from;

    public void send(String to, String subject, String content) throws Exception {
        //创建复杂邮件对象
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        //发送复杂邮件的工具类
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
        helper.setFrom(from);
        helper.setSubject(subject);
        helper.setText(content,true);
        http://img30.360buyimg.com/popWaterMark/jfs/t1/67988/7/14094/232759/5db64acfE6ab2b09e/38b5cb3dc38b4b1f.jpg"
//        helper.setText("<h1>新型冠状病毒防护守则</h1>"+
//                "<img src='http://img30.360buyimg.com/popWaterMark/jfs/t1/67988/7/14094/232759/5db64acfE6ab2b09e/38b5cb3dc38b4b1f.jpg\"' />",true);
        //添加附件
//        helper.addAttachment("罗宾.jpg",new File("C:\\Users\\hm\\Desktop\\work\\aa.jpg"));
//        helper.addAttachment("压缩文件", new File("C:\\Users\\hm\\Desktop\\20191010\\2020-02-05-智能商贸-DAY4\\resources\\resources.zip"));
        //收件人
        helper.setTo(to);
        javaMailSender.send(mimeMessage);
    }

    /**
     * 发送租户激活邮件
     * @param to
     * @param id
     */
    public void sendTenantActiveMail(String to, Long id) {
        try {
            send(to, "租户激活", "车行无忧提醒您正在激活租户<a href='http://localhost:8081/tenant/active/"+id+"'>点击激活</a>");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 租户激活失败邮件
     * @param to
     * @param message
     */
    public void sendTenantFailMail(String to, String message) {
        try {
            send(to, "租户审核未通过", "车行无忧提醒您您的租户审核未通过，审核结果："+message);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 车辆审核未通过邮件
     * @param to
     * @param message
     */
    public void sendCarFailMail(String to, String message) {
        try {
            send(to, "车辆信息审核未通过", "车行无忧提醒您您的租户审核未通过，审核结果："+message);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
